package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Message;
import com.kaishengit.entity.User;
import com.kaishengit.util.db.DBHelp;
import com.kaishengit.util.db.RowMapper;

public class MessageDao {
	
	private DBHelp<Message> db=new DBHelp<Message>();
	
	public List<Message> showMessage(){
		
		String sql="select id,content,createtime,userId,projectId from t_message";
		return db.executeQueryToList(sql, new MessageRowMapper());
	}
	
	public void showMessageByUser(User user){
		String sql="select id,content,createtime,userId,projectId from t_message where userId=?";
		db.executeQuery(sql, user.getId());
	}
	
	
	private class MessageRowMapper implements RowMapper<Message>{

		public Message mapRow(ResultSet rs) throws SQLException {
			
			Message message=new Message();
			message.setId(rs.getInt("id"));
			message.setContent(rs.getString("content"));
			message.setCreatetime(rs.getString("createtime"));
			message.setUserId(rs.getInt("userId"));
			message.setProjectId(rs.getInt("projectId"));
			
			return message;
		}	
	}
}
